package com.dropgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;
import com.dropgame.tools.ImageButton;
import com.dropgame.tools.TransitionEffect;

public class OptionsScreen implements Screen{

	private DropGame game;
	private OrthographicCamera guiCam;
	
	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
    
    public Rectangle viewport;
    
    public Input input;
    
	private SpriteBatch batcher;
	private TransitionEffect transitionEffect;

	private Sprite meshBackground;
	
	private ImageButton backButton;
	private ImageButton upSoundButton;
	private ImageButton downSoundButton;
	private ImageButton upSFXButton;
	private ImageButton downSFXButton;
	
	private BitmapFont font;
	
	private int musicLevel;
	private int sfxLevel;
	
	public OptionsScreen(DropGame game){
		this.game = game;
		
		// Create the camera with the virtual width and height.

		this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);	
		
		 //Set the camera to point at the center of the screen.
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		
		// Create viewport to be used in resizing etc.
		// The viewport is initially set to 0.0f but will be resized in resize() when screen is created.
		// This is done to prevent null pointer exceptions happening in various classes that use the viewport.
		this.viewport = new Rectangle(0.0f,0.0f,0.0f,0.0f);
		
		// Create the input.
		this.input = Gdx.input;
		
		// Create the SpriteBatch that will be used to draw our assets.
		this.batcher = new SpriteBatch();
		
		// Create the transitionEffect that is used to display an fade in and out effect when changing screens.
		this.transitionEffect = new TransitionEffect(game);
		
		
		this.meshBackground = new Sprite(AssetLoader.startBackgroundRegion);
		
		font = new BitmapFont();
		font.setScale(2.0f);
		font.setColor(1, 0, 0, 1);
		
		Sprite[] backArray = {AssetLoader.blackButton};
		Sprite[] upArray = {AssetLoader.upArrow};
		Sprite[] downArray = {AssetLoader.downArrow};
		
		this.backButton = new ImageButton(0.15f, backArray, input, guiCam, viewport, 0, 0);
		this.upSoundButton = new ImageButton(0.15f, upArray, input, guiCam, viewport, 10.0f, 350.0f);
		this.downSoundButton = new ImageButton(0.15f, downArray, input, guiCam, viewport, 10.0f, 250.0f);
		
		this.upSFXButton = new ImageButton(0.15f, upArray, input, guiCam, viewport, 165.0f, 350.0f);
		this.downSFXButton = new ImageButton(0.15f, downArray, input, guiCam, viewport, 165.0f, 250.0f);
		
		
		musicLevel = game.multipleProfile.returnDefault().volume;
		sfxLevel = game.multipleProfile.returnDefault().sfx;

	}

	@Override
	public void render(float deltaTime) {
		// Create an instance of the graphics.
		GL10 gl = Gdx.app.getGraphics().getGL10();
		
		guiCam.update();
		guiCam.apply(gl);
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
          	  		  (int) viewport.width, (int) viewport.height);
		
		// Clear the screen for the next frame to be drawn.
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		
		batcher.begin();
		meshBackground.draw(batcher);
		backButton.getKeyFrame(deltaTime).draw(batcher);
		upSoundButton.getKeyFrame(deltaTime).draw(batcher);
		downSoundButton.getKeyFrame(deltaTime).draw(batcher);
		upSFXButton.getKeyFrame(deltaTime).draw(batcher);
		downSFXButton.getKeyFrame(deltaTime).draw(batcher);
		font.draw(batcher, Integer.toString(musicLevel), 72.0f, 330.0f);
		font.draw(batcher, Integer.toString(sfxLevel), 222.0f, 330.0f);
		batcher.end();
		transitionEffect.movingScreen(batcher, deltaTime);
		updateLogic(deltaTime);
		
	}
	
	public void updateLogic(float deltaTime){
		if(backButton.getCompleted()){
			game.multipleProfile.returnDefault().volume = musicLevel;
			game.multipleProfile.returnDefault().sfx = sfxLevel;
			game.multipleProfile.persistAll();
			transitionEffect.nextScreen(new SecondMenuScreenTE(game));
		}
		
		if(upSoundButton.getCompleted()){
			if(musicLevel >= 10) musicLevel = 10;
			else musicLevel++;
		}
		
		if(downSoundButton.getCompleted()){
			if(musicLevel <= 1) musicLevel = 1;
			else musicLevel --;
		}
		
		if(upSFXButton.getCompleted()){
			if(sfxLevel >= 10) sfxLevel = 10;
			else sfxLevel ++;
		}
		
		if(downSFXButton.getCompleted()){
			if(sfxLevel <= 1) sfxLevel = 1;
			else sfxLevel --;
		}
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        //viewport = new Rectangle(crop.x, crop.y, w, h);
        viewport.set(crop.x, crop.y, w, h);
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}
}
